package edu.tyut.game.service.impl;

import edu.tyut.game.dao.game.FlyingFlowerOrderDao;
import edu.tyut.game.model.FlyingFlowerOrderData;
import edu.tyut.game.model.UserTotalRoundsRank;
import edu.tyut.game.model.UserWinRateRank;
import edu.tyut.game.service.FlyingFlowerOrderService;
import edu.tyut.game.utils.GetContents;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class FlyingFlowerOrderServiceImpl implements FlyingFlowerOrderService {

    private FlyingFlowerOrderDao orderDao;
    @Autowired
    public void setOrderDao(FlyingFlowerOrderDao orderDao) {
        this.orderDao = orderDao;
    }

    @Override
    public FlyingFlowerOrderData getFlyingFlowerOrderUserData(int uid) {
        return orderDao.getUserDataByUid(uid);
    }

    @Override
    public void firstAddData(int uid) {
        orderDao.insertData(uid);
    }

    @Override
    public int updateData(FlyingFlowerOrderData data) {
        return orderDao.updateData(data);
    }

    @Cacheable(cacheNames = "words", key = "#word")
    @Override
    public List<String> getContentsByWord(String word) {
        List<String> contents = orderDao.getContentsByWord(word);
        List<String> words = GetContents.getWords(contents, word);
        log.info("包含 【" + word + "】的诗句共有: " + words.size());
        log.info(words.toString());
        // http://localhost:8080/flyingFlowerOrderGame/getContents/山
        return words;
    }

    @Override
    public List<UserWinRateRank> getWinRateRank() {
        return orderDao.getWinRateRank();
    }

    @Override
    public List<UserTotalRoundsRank> getTotalRoundsRank() {
        return orderDao.getTotalRoundsRank();
    }
}
